import axiosIns from '../../utils/axios';
import { orderList } from '../../utils/mock';
import { ACTIONSTATE } from '../../utils/constant';
/**
|--------------------------------------------------
|  Constants
|--------------------------------------------------
*/
const ORD_LOADING = 'ORD_LOADING';
const LOADED = 'LOADED';
const ORDERLIST = 'ORDERLIST';
const DATASOURCE = 'DATASOURCE';
const ORDERINFO = 'ORDERINFO';
/**
|--------------------------------------------------
| Actions
|--------------------------------------------------
*/
/**获取最新全部的订单列表 账户首页*/
export function getOrderInfo() {
  return async (dispatch, getState) => {
    const loading = getState().order.loading;
    if (loading) return;
    dispatch(fetching());
    const res = await axiosIns('/getOrderList');
    dispatch(datasource(res.data));
    dispatch(orderInfo());
  };
}

/**我的订单 */
export function getOrderList(params) {
  return async (dispatch, getState) => {
    const res = await axiosIns('/getOrderList');
    dispatch(datasource(res.data));
    dispatch(filter(params));
  };
}

/**根据条件对已有列表数据进行筛选 */
export function filter(params) {
  return async (dispatch, getState) => {
    const loading = getState().order.loading;
    if (loading) return;
    dispatch(fetching());
    const datasource = getState().order.datasource;
    if (!datasource) return;
    let list = [].concat(datasource); //不能改变源数组;
    const { state, startDate, endDate, orderNo, contactor } = params;
    //先过滤状态
    if (state != 'all') {
      list = list.filter(item => item.state == state);
    }
    //再过滤搜索条件
    if (orderNo) {
      list = list.filter(item => item.orderno.toString().indexOf(orderNo) > -1);
    }
    if (contactor) {
      list = list.filter(item => item.customer.indexOf(contactor) > -1);
    }
    if (startDate && endDate) {
      list = list.filter(
        item =>
          new Date(item.useTime).getTime() > new Date(startDate).getTime() &&
          new Date(item.useTime).getTime() < new Date(endDate).getTime()
      );
    }
    dispatch(orderListed({ list }));
  };
}

/**账户首页 */
export function orderInfo() {
  return async (dispatch, getState) => {
    const datasource = getState().order.datasource;
    if (!datasource) return;
    let list = [].concat(datasource); //不能改变源数组;
    list = list.splice(0, 3);
    let unpay = datasource.filter(item => item.state == ACTIONSTATE[0].type)
      .length;
    let unComment = datasource.filter(item => item.state == ACTIONSTATE[1].type)
      .length;
    let unTravel = datasource.filter(item => item.state == ACTIONSTATE[3].type)
      .length;
    dispatch(orderInfoed({ unpay, unTravel, unComment }));
    dispatch(orderListed({ list }));
  };
}

function fetching() {
  return {
    type: ORD_LOADING
  };
}
function orderListed(data) {
  return {
    type: ORDERLIST,
    list: data.list
  };
}

function orderInfoed(data) {
  return {
    type: ORDERINFO,
    data: data
  };
}
function datasource(data) {
  return {
    type: DATASOURCE,
    list: data.list,
    totalNum: data.count
  };
}

/**
|--------------------------------------------------
| Action Handlers
|--------------------------------------------------
*/

const ACTION_HANDLERS = {
  [ORD_LOADING]: (state, action) => {
    console.log('order loading');
    return { ...state, loading: true };
  },
  [ORDERLIST]: (state, action) => {
    return {
      ...state,
      loading: false,
      list: action.list
    };
  },
  [DATASOURCE]: (state, action) => {
    return {
      ...state,
      datasource: action.list,
      loading: false,
      totalNum: action.totalNum
    };
  },
  [ORDERINFO]: (state, action) => {
    const { data } = action;
    return {
      ...state,
      unpay: data.unpay,
      unTravel: data.unTravel,
      unComment: data.unComment
    };
  }
};
/**
|--------------------------------------------------
| Reducer
|--------------------------------------------------
*/
const initialState = {
  loading: false,
  totalNum: 0,
  list: null,
  unpay: 0,
  unComment: 0,
  unTravel: 0,
  datasource: null
};
export default function(state = initialState, action) {
  const handler = ACTION_HANDLERS[action.type];
  return handler ? handler(state, action) : state;
}
